package com.irdstudio.apicenter.gateway.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.irdstudio.apicenter.gateway.comm.MaccsChannelFactory;
import com.irdstudio.apicenter.gateway.core.dm.AppBaseInfo;
import com.irdstudio.apicenter.gateway.core.dm.AppChannelAccess;
import com.irdstudio.apicenter.gateway.core.dm.AppChannelForward;
import com.irdstudio.apicenter.gateway.core.dm.AppTradeCommon;
import com.irdstudio.apicenter.gateway.core.dm.AppTradeInout;
import com.irdstudio.apicenter.gateway.core.dm.AppTradePrivate;
import com.irdstudio.apicenter.gateway.core.jdbc.MaccsBaseDAO;
import com.irdstudio.apicenter.gateway.util.NullUtil;

/**
 * 通讯控制总线辅助类（读取总线下交易、通道、报文要素）
 * 
 * @author guangming.li
 * @version 1.0
 * @date 2015-05-20
 */
public class MaccsControllerBusUtil {

	/* 文本日志记录对象(Logback对象) */
	private final static Logger logger = LoggerFactory
			.getLogger(MaccsControllerBusUtil.class);

	/**
	 * 读取指定移动应用的配置信息到通讯控制总线类中
	 * 
	 * @param appId
	 * @param conn
	 * @return
	 */
	public static MaccsControllerBus loadBusConfigure(String appId,
			MaccsBaseDAO baseDAO) {

		MaccsControllerBus ctrlBus = null;

		logger.info("读取APP应用基本信息...");
		ctrlBus = new MaccsControllerBus(baseDAO.findBean(AppBaseInfo.class,
				new Object[] { appId }));

		logger.info("读取通讯控制总线的渠道接入配置信息...");

		AppChannelAccess aca = baseDAO.findBean(AppChannelAccess.class,
				new Object[] { appId });
		Assert.notNull(aca, "未找到[" + appId + "]应用的接入设置信息");

		ctrlBus.setChannelAccess(aca);

		logger.info("读取通讯控制总线下的所有转发渠道配置信息...");
		List<AppChannelForward> acfList = baseDAO
				.findBeanList(AppChannelForward.class);
		ctrlBus.setChannelForwardList(acfList);
		for (AppChannelForward acf : acfList) {
			acf.setChannelHeadDefine(NullUtil.formatNull(acf
					.getChannelHeadDefine()));
			MaccsChannelFactory.addChannelDefine(acf);
		}

		logger.info("读取移动应用综合管理平台下支持的公共交易配置信息...");
		List<AppTradeCommon> tradeCommonList = baseDAO
				.findBeanList(AppTradeCommon.class);
		for (AppTradeCommon trade : tradeCommonList) {
			MaccsTradeDefineVo mtdv = new MaccsTradeDefineVo();
			mtdv.setAppId(appId);
			mtdv.setTradeId(trade.getTradeCode());
			mtdv.setTradeCode(trade.getTradeCode());
			mtdv.setTradeName(trade.getTradeName());
			mtdv.setTradeClass(trade.getTradeClass());
			mtdv.setUseBaffle(false);
			mtdv.setCheckToken(false);
			mtdv.setCheckDevice(false);
			mtdv.setCheckNetwork(false);
			ctrlBus.addMaccsTradeDefine(trade.getTradeCode(), mtdv);
			logger.info("----Add Trade:" + trade.getTradeCode() + ","
					+ trade.getTradeName() + "...");
		}

		logger.info("读取通讯控制总线下支持的交易配置信息...");
		List<AppTradePrivate> tradePrivateList = baseDAO.findBeanList(
				"select * from app_trade_private where app_id = ?",
				AppTradePrivate.class, new Object[] { appId });

		for (AppTradePrivate trade : tradePrivateList) {
			MaccsTradeDefineVo mtdv = new MaccsTradeDefineVo();
			mtdv.setAppId(appId);
			mtdv.setTradeId(trade.getTradeId());
			mtdv.setTradeCode(trade.getTradeCode());
			mtdv.setTradeName(trade.getTradeName());
			mtdv.setTradeClass("com.yuchengtech.maccs.web.controller.TradeForward");
			mtdv.setForwardChannelId(trade.getForwardChannelId());
			if ("Y".equals(trade.getIsUseBaffle())) {
				mtdv.setUseBaffle(true);
				mtdv.setTradeBaffleData(trade.getTradeBaffleData());
			} else {
				mtdv.setUseBaffle(false);
			}
			mtdv.setCheckToken("Y".equals(trade.getIsCheckToken()) ? true : false);
			mtdv.setCheckDevice("Y".equals(trade.getIsCheckDevice()) ? true : false);
			mtdv.setCheckNetwork("Y".equals(trade.getIsCheckNetwork()) ? true : false);
			
			//读取交易下输入输出格式格式配置
			List<AppTradeInout> tradeInoutList = baseDAO.findBeanList(
					"select * from app_trade_inout where trade_id=?",
					AppTradeInout.class, new Object[] { trade.getTradeId()});
			if (tradeInoutList != null && tradeInoutList.size() > 0) {
				mtdv.setInoutList(tradeInoutList);
			}	
			
			ctrlBus.addMaccsTradeDefine(trade.getTradeCode(), mtdv);
			logger.info("----Add Trade:" + trade.getTradeCode() + ","
					+ trade.getTradeName() + "...");
		}

		ctrlBus.setReady(true);

		return ctrlBus;
	}

}
